package com.sghd.candy.module.element.manager;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.*;
import com.sghd.common.ramcache.enhance.Enhance;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.pojo.fight.game.ChessType;

import javax.persistence.*;
import java.util.Map;

/**
 * @author Allen
 * @date 2018/12/14
 */
@Entity
@Table(catalog = "bsdld")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_HOUR))
@InitialConfig(type = InitialType.ALL)
public class ElementConfig implements IEntity<ChessType>, PerPersist {
    @Id
    private ChessType eleId;
    @Column(length = 2)
    private String eleName;
    /**掉率*/
    private Float dropRate;
    /**升级配置*/
    @Transient
    private Map<Integer, UpgradeEle> upgradeConfig;
    @Lob
    private String upgradeConfigJson;

    public ChessType getEleId() {
        return eleId;
    }

    void setEleId(ChessType eleId) {
        this.eleId = eleId;
    }

    public String getEleName() {
        return eleName;
    }

    void setEleName(String eleName) {
        this.eleName = eleName;
    }

    public Float getDropRate() {
        return dropRate;
    }

    void setDropRate(Float dropRate) {
        this.dropRate = dropRate;
    }

    public Map<Integer, UpgradeEle> getUpgradeConfig() {
        return upgradeConfig;
    }

    void setUpgradeConfig(Map<Integer, UpgradeEle> upgradeConfig) {
        this.upgradeConfig = upgradeConfig;
    }

    public String getUpgradeConfigJson() {
        return upgradeConfigJson;
    }

    void setUpgradeConfigJson(String upgradeConfigJson) {
        this.upgradeConfigJson = upgradeConfigJson;
    }

    @Enhance
    void config(float dropRate) {
            this.dropRate = dropRate;
    }

    @Enhance
    void config(int level, UpgradeEle upgradeEle) {
        this.upgradeConfig.put(level, upgradeEle);
    }

    @Override
    public ChessType getId() {
        return this.eleId;
    }

    @Override
    public void afterLoad() {
        upgradeConfig = JsonUtils.string2Map(upgradeConfigJson, Integer.class, UpgradeEle.class);
    }

    @Override
    public void beforePersist() {
        upgradeConfigJson = JsonUtils.object2String(upgradeConfig);
    }
}
